package com.zhiy.com.estation.ui.base;

import android.content.Context;
import android.os.Binder;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;


import com.zhiy.com.estation.R;
import com.zhiy.com.estation.manager.PreferenceManager;
import com.zhiy.com.estation.wigdet.AppTitleBar;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * All Resered by My Zong  @2016-2017
 */
public abstract class BaseFragment extends Fragment {
    private LayoutInflater mInflaterLayoutId;
    protected AppTitleBar mTitleBar;
    protected String RECEIVER="receiver";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getInflaterLayoutId(), container, false);
    }
    protected Context mContext;
    protected BaseActivity mBaseActivity;
    private Unbinder mUnbinder;
    protected Bundle mBundle;
    //    输入管理工具
    protected InputMethodManager mInputMethodManager;
    protected PreferenceManager mPreferen;
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.setClickable(true);
        //共享文件的创和获取共享文件
        mPreferen=PreferenceManager.getInstance();
        mContext = getContext();
        mBaseActivity = (BaseActivity) getActivity();
//        填充当前布局
        mUnbinder = ButterKnife.bind(this,view);
//        添加TitleBar
        mTitleBar = (AppTitleBar) view.findViewById(R.id.apptitle_root);
//        防止点击穿透，底层fragment响应顶层点击事件
        initView(view);
        setBackListener();

    }

    @Override
    public void onPause() {
        super.onPause();
        getView().setClickable(false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }
    protected abstract int getInflaterLayoutId();

    protected abstract void initView(View view);

    /**
     * 隐藏虚拟键盘
     */
    protected void hideSoftKeyboard() {
        BaseActivity activity = mBaseActivity;
        if (mBaseActivity.getWindow().getAttributes().softInputMode !=
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (activity.getCurrentFocus() != null)
                if (mInputMethodManager == null) {
//                管理软键盘，在需要使用的fragment中作初始化  在哪里使用在哪里初始化
                    mInputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                }
            mInputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    protected void setBackListener() {
        if (mTitleBar != null) {
            mTitleBar.getBack().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popBackStack();
                }
            });
        }
    }

    protected void popBackStack() {
        if (isDetached() || isRemoving() || getFragmentManager() == null) {
            return;
        }
        if (isResumed()) {
            getFragmentManager().popBackStackImmediate();
        }
    }


}
